package game;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;

public class Button extends Sprite {
	
	private static final long serialVersionUID = 1L;
	public boolean clicked, released;
	private Point2D initSourcePos;
	private Point2D sourcePosition;
	private Point2D sourceDimension;

	public Button(int index, Point2D pos, Point2D dim, Point2D sPos, Point2D sDim) {
		super(index, pos, dim);
		// TODO Auto-generated constructor stub
		this.sourcePosition = sPos;
		this.sourceDimension = sDim;
		initSourcePos = sourcePosition;
	}

	public void update()
	{
		released = false;
		
		if (new Rectangle((int)getPosition().getX(), (int)getPosition().getY(), (int)getDimension().getX(), (int)getDimension().getY()).contains
		   (Mouse.getInstance().getPositionMouse().x, Mouse.getInstance().getPositionMouse().y))
		{
			this.sourcePosition = new Point2D.Double(this.sourcePosition.getX(), this.initSourcePos.getY() + this.sourceDimension.getY());
			if (Mouse.getInstance().isMousePressed(MouseEvent.BUTTON1))
			{
				clicked = true;
				released = false;
			}
			if (Mouse.getInstance().isMousePressed(MouseEvent.BUTTON1) == false && clicked == true)
			{
				clicked = false;
				released = true;
			}
		}
		else
		{
			this.sourcePosition = new Point2D.Double(this.sourcePosition.getX(), this.initSourcePos.getY());
			clicked = false;
			released = false;
		}
	}
	
	public void draw(Graphics g)
	{
		super.draw(g, sourcePosition, sourceDimension);
	}
}
